package profile

import (
	"net/http"

	"usercenter/internal/logic/profile"
	"usercenter/internal/svc"
	"usercenter/internal/types"

	"github.com/zeromicro/go-zero/rest/httpx"
)

// 统一错误响应结构
type ErrorResponse struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

func GetUserInfoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.UserIdRequest
		if err := httpx.Parse(r, &req); err != nil {
			// 参数解析错误
			httpx.WriteJsonCtx(r.Context(), w, http.StatusBadRequest, ErrorResponse{
				Code:    -1,
				Message: "参数解析错误: " + err.Error(),
			})
			return
		}

		l := profile.NewGetUserInfoLogic(r.Context(), svcCtx)
		resp, err := l.GetUserInfo(&req)
		if err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
		} else {
			httpx.OkJsonCtx(r.Context(), w, resp)
		}
	}
}
